﻿Type.registerNamespace("EcmaDoc");

EcmaDoc.Index = function Index()
{
	this.updateDelay = 250;
	this.membersPerPage = 200;
	this.timeoutID = null;

	this.originalData = null;
	this.currentData = null;

	this.indexLetters = "$_ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	this.pageNumber = 0;
}

EcmaDoc.Index.prototype.setupInstance = function Index$setupInstance()
{
	$xml.loadXmlDocument("index.xml",
			Function.createDelegate(this, this.onXmlLoadSuccess), /**/
			Function.createDelegate(this, this.onXmlLoadError)); /**/
}

EcmaDoc.Index.prototype.initializeData = function Index$initializeData(document)
{
	var nodes = $xml.selectNodes(document, "/project/index/*");
	var data = []
	var item, key;
	for (var i = 0; i < nodes.length; i++)
	{
		item = {
			name: nodes[i].getAttribute("name"),
			fullName: nodes[i].getAttribute("fullName"),
			namePrefix: nodes[i].getAttribute("namePrefix"),
			visibility: nodes[i].getAttribute("visibility"),
			indexName: nodes[i].getAttribute("indexName"),
			searchName: nodes[i].getAttribute("indexName").toLowerCase(),
			memberType: nodes[i].localName,
			url: nodes[i].getAttribute("url")
		};
		data.push(item);
	}

	this.originalData = data;
	this.setCurrentData(this.originalData);
	this.createLetterIndex();
}

EcmaDoc.Index.prototype.initializeInterface = function Index$initializeInterface()
{
	$("#member_name").value = String.EMPTY;

	$evt.addListener($("#member_name"), "onkeyup", Function.createDelegate(this, this.onFilterKeyUp)); /**/
	$evt.addListener($("#pager .prev"), "onclick", Function.createDelegate(this, this.onPrevPageClick)); /**/
	$evt.addListener($("#pager .next"), "onclick", Function.createDelegate(this, this.onNextPageClick)); /**/

	var letters = $$("#index_letters div");
	letters.each(function (el)
	{
		el.letter = $dom.innerText(el).trim().toUpperCase();
	});

	$evt.addListener(letters, "onclick", Function.createDelegate(this, this.onLetterIndexClick)); /**/
}

EcmaDoc.Index.prototype.clearDataFilter = function Index$clearDataFilter()
{
	this.setCurrentData(this.originalData);
}

EcmaDoc.Index.prototype.setCurrentData = function Index$setCurrentData(data)
{
	this.currentData = data;

	this.updatePager(true);
	this.showPageNumber(1);
}

EcmaDoc.Index.prototype.updatePager = function Index$updatePager(redraw)
{
	var pageCount = Math.ceil(this.currentData.length / this.membersPerPage);

	if (pageCount == 0)
	{
		$css.undisplay($("#pager"));
		return;
	}
	else
	{
		$css.display($("#pager"));
	}

	var pageList = $("#pager .pages");
	var pagePrev = $("#pager .prev");
	var pageNext = $("#pager .next");

	if (redraw)
	{
		var onPageClick = Function.createDelegate(this, this.onPageNumberClick); /**/

		$evt.removeListener($$("span", pageList), "onclick", onPageClick);

		pageList.innerHTML = String.EMPTY;
		for (var i = 1; i <= pageCount; i++)
		{
			var s = pageList.appendChild(document.createElement("span"));
			s.className = "link";
			s.innerHTML = i;
			s.page = i;
			$evt.addListener(s, "onclick", onPageClick);
		}
	}

	var links = $$("span", pageList);
	for (var i = 0; i < links.length; i++)
	{
		if (links[i].page == this.pageNumber)
			$css.addClass(links[i], "current");
		else
			$css.removeClass(links[i], "current");
	}

	this.setElementEnabled(pagePrev, this.pageNumber != 1);
	this.setElementEnabled(pageNext, this.pageNumber != pageCount);
}

EcmaDoc.Index.prototype.createLetterIndex = function Index$createLetterIndex()
{
	var letterIndex = {};
	var l;
	for (var i = 0; i < this.originalData.length; i++)
	{
		l = this.originalData[i].searchName[0].toUpperCase();
		letterIndex[l] = l;
	}

	var elems = $$("#index_letters div");
	for (var i = 0; i < elems.length; i++)
	{
		this.setElementEnabled(elems[i], letterIndex[elems[i].letter] != null);
	}
}

EcmaDoc.Index.prototype.showPageNumber = function Index$showPageNumber(number)
{
	this.pageNumber = number;

	var listElement = $("#index_members");
	listElement.innerHTML = String.EMPTY;

	var startIndex = (number - 1) * this.membersPerPage;
	var endIndex = number * this.membersPerPage;

	for (var i = startIndex; i < endIndex; i++)
	{
		var indexMember = this.currentData[i];
		if (!indexMember)
			break;

		var prevMember = this.currentData[i - 1];
		var nextMember = this.currentData[i + 1];

		var prevDuplicate = (prevMember && prevMember.indexName == indexMember.indexName);
		var nextDuplicate = (nextMember && nextMember.indexName == indexMember.indexName);

		var itemElement = listElement.appendChild(document.createElement("li"));
		itemElement.className = indexMember.memberType;

		var iconElement = itemElement.appendChild(document.createElement("div"));
		$css.addClass(iconElement, "icon");
		$css.addClass(iconElement, indexMember.memberType);
		$css.addClass(iconElement, indexMember.visibility);

		var linkText = (prevDuplicate || nextDuplicate)
			? String.format("{0} ({1})", indexMember.indexName, indexMember.namePrefix)
			: indexMember.indexName;

		var linkElement = itemElement.appendChild(document.createElement("a"));
		linkElement.href = indexMember.url;
		linkElement.target = "main";
		linkElement.innerHTML = linkText;
		linkElement.title = String.format("{0} ({1})", indexMember.fullName, indexMember.memberType);
	}

	this.updatePager();
}

EcmaDoc.Index.prototype.filterTo = function Index$filterTo(value)
{
	var resultData = [];
	var searchAnywhere = false;
	if (value.indexOf("*") == 0)
	{
		searchAnywhere = true;
		value = value.trim().trim("\\*");
	}

	value = value.toLowerCase();

	for (var i = 0; i < this.originalData.length; i++)
	{
		var index = this.originalData[i].searchName.indexOf(value);
		if ((!searchAnywhere && index == 0) || (searchAnywhere && index > -1))
		{
			resultData.push(this.originalData[i]);
		}
	}

	resultData.sort(function (a, b)
	{
		var i1 = a.searchName.indexOf(value);
		var i2 = b.searchName.indexOf(value);

		if (i1 < i2)
			return -1;
		if (i1 > i2)
			return 1;

		return 0;
	});

	this.setCurrentData(resultData);
}

EcmaDoc.Index.prototype.isDisabled = function Index$isDisabled(elem)
{
	return $css.containsClass(elem, "disabled");
}

EcmaDoc.Index.prototype.setElementEnabled = function Index$setElementEnabled(elem, enabled)
{
	if (enabled)
		this.enable(elem);
	else
		this.disable(elem);
}

EcmaDoc.Index.prototype.enable = function Index$enable(elem)
{
	$css.removeClass(elem, "disabled");
	elem.disabled = false;
}

EcmaDoc.Index.prototype.disable = function Index$disable(elem)
{
	$css.addClass(elem, "disabled");
	elem.disabled = true;
}

EcmaDoc.Index.prototype.onPrevPageClick = function Index$onPrevPageClick(e)
{
	var elem = $evt.srcElement(e);
	elem = $dom.parentOrSelf(elem, ".prev");
	if (this.isDisabled(elem))
		return;

	var pageNumber = this.pageNumber - 1;
	this.showPageNumber(pageNumber);
}

EcmaDoc.Index.prototype.onNextPageClick = function Index$onNextPageClick(e)
{
	var elem = $evt.srcElement(e);
	elem = $dom.parentOrSelf(elem, ".next");
	if (this.isDisabled(elem))
		return;

	var pageNumber = this.pageNumber + 1;
	this.showPageNumber(pageNumber);
}

EcmaDoc.Index.prototype.onPageNumberClick = function Index$onPageNumberClick(e)
{
	var pageNumber = parseInt($dom.innerText($evt.srcElement(e)));
	this.showPageNumber(pageNumber);
}

EcmaDoc.Index.prototype.onLetterIndexClick = function Index$onLetterIndexClick(e)
{
	var elem = $evt.srcElement(e);
	if (this.isDisabled(elem))
		return;

	var letter = $dom.innerText(elem).trim();
	this.filterTo(letter, true);

	$("#member_name").value = letter;
	$("#member_name").focus();
	$("#member_name").select();
}

EcmaDoc.Index.prototype.onFilterKeyUp = function Index$onFilterKeyUp(e)
{
	if (this.timeoutID != null)
		window.clearTimeout(this.timeoutID);

	var self = this;
	this.timeoutID = Function.setTimeout(this.updateDelay, function ()
	{
		var value = $("#member_name").value.trim();
		if (value == String.EMPTY)
			self.clearDataFilter();
		else
			self.filterTo(value);
	});
}

EcmaDoc.Index.prototype.onXmlLoadSuccess = function Index$onXmlLoadSuccess(document)
{
	this.initializeInterface();
	this.initializeData(document);
}

EcmaDoc.Index.prototype.onXmlLoadError = function Index$onXmlLoadError(evt)
{
	var index_error = $("#index_error");
	var index_data = $("#index_data");

	index_error.innerHTML = evt.data.message;

	$css.display(index_error);
	$css.undisplay(index_data);
}

EcmaDoc.Index.setup = function Index$setup()
{
	EcmaDoc.Index.instance = new EcmaDoc.Index();
	EcmaDoc.Index.instance.setupInstance();
}

$evt.addListener(window, "onload", EcmaDoc.Index.setup);

// parse index members

// 1. show all
	// set current members (all)
// 2. search
	// set current members (result)
// 3. letter index
	// set current members (result = starts with letter)

// set current members
	// draw controls
		// draw letter index
		// draw page controls
	// draw page (1)
		// update page controls

